import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _concat from "lodash/concat";
import _slice from "lodash/slice";
import _isNumber from "lodash/isNumber";
import _cloneDeep from "lodash/cloneDeep";
import _isFunction from "lodash/isFunction";
import _map from "lodash/map";

var _dec, _class;

/**
 * index
 * @date 2018/10/10
 */
import React from 'react';
import { Bind } from 'lodash-decorators';
import Row from "./Row";
import styles from "../../index.less?modules";
import DrawDragField from "../../Drag/DrawDragField";
import { emptyField, emptyFieldType, fieldLabelProp, fieldNameProp } from "../../../config";
import DrawDragComponent from "../../Drag/DrawDragComponent";
var DynamicFormRender = (_dec = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicFormRender, _React$Component);

  var _super = _createSuper(DynamicFormRender);

  function DynamicFormRender(props) {
    var _this;

    _classCallCheck(this, DynamicFormRender);

    _this = _super.call(this, props);
    _this.wrapperFieldComponent = _this.wrapperFieldComponent.bind(_assertThisInitialized(_this));
    _this.handleSwapField = _this.handleSwapField.bind(_assertThisInitialized(_this));
    _this.handleRemoveField = _this.handleRemoveField.bind(_assertThisInitialized(_this));
    _this.handleAddField = _this.handleAddField.bind(_assertThisInitialized(_this));
    _this.handleUpdateComponent = _this.handleUpdateComponent.bind(_assertThisInitialized(_this));
    _this.state = {
      willRemovePositionX: -1
    };
    _this.handleUpdateWillPositionX = _this.handleUpdateWillPositionX.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(DynamicFormRender, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$component = _this$props.component,
          component = _this$props$component === void 0 ? {} : _this$props$component,
          fieldLabelWidth = _this$props.fieldLabelWidth,
          currentEditField = _this$props.currentEditField,
          currentEditComponent = _this$props.currentEditComponent,
          onActiveComponent = _this$props.onActiveComponent;
      var willRemovePositionX = this.state.willRemovePositionX;
      var rowCount = component.fields.length;
      var lineNumber = 0;

      var rows = _map(component.fields, function (field, rowIndex) {
        lineNumber += 1;
        return /*#__PURE__*/React.createElement(Row, {
          key: lineNumber,
          wrapperFieldComponent: _this2.wrapperFieldComponent,
          fieldLabelWidth: fieldLabelWidth,
          rowIndex: rowIndex,
          positionY: rowIndex,
          component: component,
          onRemoveField: _this2.handleRemoveField,
          cols: field,
          rowCount: rowCount,
          currentEditField: currentEditField,
          onUpdateComponent: _this2.handleUpdateComponent // 删除的位置
          ,
          willRemovePositionX: willRemovePositionX,
          onUpdateWillRemovePositionX: _this2.handleUpdateWillPositionX,
          onRefreshTemplate: _this2.handleRefresh
        });
      });

      return /*#__PURE__*/React.createElement("div", {
        className: styles['dynamic-form']
      }, /*#__PURE__*/React.createElement(DrawDragComponent, {
        component: component,
        onActiveComponent: onActiveComponent,
        currentEditComponent: currentEditComponent,
        currentEditField: currentEditField
      }, rows));
    }
  }, {
    key: "wrapperFieldComponent",
    value: function wrapperFieldComponent(fieldElement, field, fieldOptions) {
      var _this$props2 = this.props,
          component = _this$props2.component,
          onActiveField = _this$props2.onActiveField,
          currentEditField = _this$props2.currentEditField;
      return /*#__PURE__*/React.createElement(DrawDragField, {
        pComponent: component,
        component: field,
        onSwapField: this.handleSwapField,
        onRemoveField: this.handleRemoveField,
        onAddField: this.handleAddField,
        onActiveField: onActiveField,
        fieldOptions: fieldOptions,
        currentEditField: currentEditField
      }, fieldElement);
    }
  }, {
    key: "handleSwapField",
    value: function handleSwapField(component, dragField, dropField, dragFieldOptions, dropFieldOptions) {
      // 保证数据一定正确
      var tempField = component.fields[dragFieldOptions.rowIndex][dragFieldOptions.colIndex];
      var tempFieldColspan = tempField.colspan; // 交换 colspan
      // eslint-disable-next-line

      component.fields[dragFieldOptions.rowIndex][dragFieldOptions.colIndex].colspan = component.fields[dropFieldOptions.rowIndex][dropFieldOptions.colIndex].colspan; // eslint-disable-next-line

      component.fields[dropFieldOptions.rowIndex][dropFieldOptions.colIndex].colspan = tempFieldColspan; // 交换 field
      // eslint-disable-next-line

      component.fields[dragFieldOptions.rowIndex][dragFieldOptions.colIndex] = component.fields[dropFieldOptions.rowIndex][dropFieldOptions.colIndex]; // eslint-disable-next-line

      component.fields[dropFieldOptions.rowIndex][dropFieldOptions.colIndex] = tempField;
      this.handleRefresh();
    }
    /**
     * todo 要将 site 字段放到 removeSiteFields 中
     * @param {object} component
     * @param {object} field
     * @param {object} fieldOptions
     */

  }, {
    key: "handleRemoveField",
    value: function handleRemoveField(component, field, fieldOptions) {
      // 保证数据一定正确
      // TODO 跨行的处理
      if (field.colspan && _isNumber(field.colspan)) {
        var fillFields = [];

        for (var index = 0; index < field.colspan; index += 1) {
          fillFields.push(_cloneDeep(emptyField));
        } // eslint-disable-next-line


        component.fields[fieldOptions.rowIndex] = _concat([], _slice(component.fields[fieldOptions.rowIndex], 0, fieldOptions.colIndex), fillFields, _slice(component.fields[fieldOptions.rowIndex], fieldOptions.colIndex + 1));
      } else {
        // eslint-disable-next-line
        component.fields[fieldOptions.rowIndex][fieldOptions.colIndex] = _cloneDeep(emptyField);
      } // // todo 租户级代码
      // if(field.siteFlag === 1) {
      //   // 平台级 需要放在 removeSiteFields 中
      //   // eslint-disable-next-line no-param-reassign
      //   field.visiableFlag = 0;
      //   component.removeSiteFields.push(field);
      // }


      this.handleRefresh();
    }
  }, {
    key: "handleAddField",
    value: function handleAddField(component, dragField, dropField, dragFieldOptions, dropFieldOptions) {
      var onUpdateComponent = this.props.onUpdateComponent;

      if (dropField.componentType !== emptyFieldType) {
        // TODO 只能在 空 的地方添加新的字段, 非空的也进不来, 因为有 canDrop
        return;
      }

      var newField = _cloneDeep(dragField);

      var fieldProps = []; // 字段本身的属性

      newField.config = fieldProps;
      newField[fieldLabelProp] = newField.name;
      newField[fieldNameProp] = 'defaultCode'; // 删除 field 的其他属性

      delete newField.className;
      delete newField.type;
      delete newField.defaultProps;
      delete newField.name;
      delete newField.props; // 字段在容器中的属性

      switch (component.templateType) {
        case 'DynamicForm':
          // 处理字段在表单中的属性
          break;

        default:
          break;
      } // eslint-disable-next-line


      component.fields[dropFieldOptions.rowIndex][dropFieldOptions.colIndex] = newField; // TODO 更新 PageDesigner 中的数据

      if (_isFunction(onUpdateComponent)) {
        onUpdateComponent(component);
      } // TODO remove next line after debug complete


      this.forceUpdate();
    }
  }, {
    key: "handleUpdateWillPositionX",
    value: function handleUpdateWillPositionX() {
      var willRemovePositionX = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
      this.setState({
        willRemovePositionX: willRemovePositionX
      });
    }
  }, {
    key: "handleUpdateComponent",
    value: function
      /* component */
    handleUpdateComponent() {
      // TODO 更新 component;
      this.forceUpdate();
    }
  }, {
    key: "handleRefresh",
    value: function handleRefresh() {
      this.forceUpdate();
    }
  }]);

  return DynamicFormRender;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleRefresh", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleRefresh"), _class.prototype)), _class));
export { DynamicFormRender as default };